<template>
  <Form autocomplete="off" class="user-form" ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
    <FormItem v-if="isAdd" label="所属单位" prop="orgId">
      <TreeSelect filterable width="100%" :data="orgList"  v-model="formValidate.orgId" placeholder="请选择上级单位"></TreeSelect>
    </FormItem>
    <FormItem label="用户姓名" prop="name" >
      <Input   name="name"  v-model="formValidate.name" placeholder="请输入单位名称"></Input>
    </FormItem>
<!--    <FormItem   label="用户性别" prop="sex" >-->
<!--      <RadioGroup name="sex" v-model="formValidate.sex">-->
<!--        <Radio key="sex1" :label="1" >男</Radio>-->
<!--        <Radio key="sex2" :label="2" >女</Radio>-->
<!--      </RadioGroup>-->
<!--    </FormItem>-->
    <FormItem  label="用户权限" prop="type" >
      <RadioGroup name="type" v-model="formValidate.type">
        <Radio :key="item.code" v-for="item in jurisdictionList" :label="item.code" >{{item.name}}</Radio>
      </RadioGroup>
    </FormItem>
<!--    <FormItem label="家庭住址" prop="address">-->
<!--      <Input v-model="formValidate.address"   placeholder="请输入家庭住址"></Input>-->
<!--    </FormItem>-->
    <FormItem label="城市区域" prop="regionCode">
      <Cascader filterable width="100%" :data="list"  v-model="formValidate.regionCode" placeholder="请选择城市区域"></Cascader>
    </FormItem>
    <FormItem v-if="isAdd" label="手机号码" prop="phone">
      <Input v-model="formValidate.phone"   placeholder="请输入手机号"></Input>
    </FormItem>
    <FormItem v-if="isAdd" label="登录密码" prop="password">
      <Input v-model="formValidate.password"   placeholder="请输入登录密码"></Input>
    </FormItem>
    <div class="ivu-text-center mt20">
      <Button  @click="handleSubmit('formValidate')">提交</Button>
    </div>
  </Form>
</template>
<script>
import {userAdd,userUpdate} from '@/api/userManage';
import {validatorTel} from '@/libs/validator';
export default {
  created() {

  },
  props:{
    list:{
      type:Array,
      default:()=>{
        return []
      }
    },
    orgList:{
      type:Array,
      default:()=>{
        return []
      }
    },
    jurisdictionList:{
      type:Array,
      default:()=>{
        return []
      }
    },
    isAdd:{
      type:Boolean,
      default:true,
    },
    editorData:{
      type:Object,
      default:()=>{
        return {};
      }
    },
  },
  watch:{
    isAdd(newdata,oldData){
      this.init();
    }
  },
  mounted() {
    this.init()
  },
  data () {
    return {
      pwdType:'text',
      formValidate: {
        orgId:'',
        name:'',
        // sex:0,
        type:'',
        address:'',
        regionCode:[],
        phone:'',
        password:'',
        id:'',
      },
      ruleValidate: {
        orgId: [
          { required: true,  message: '单位所属单位不能为空', trigger: 'change' },
        ],
        name: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { max: 100, message: '用户姓名长度不能超过100个字符', trigger: 'blur' },
        ],
        // sex: [
        //   { required: true,type:'number',  message: '用户性别必须选择', trigger: 'change' },
        // ],
        type: [
          { required: true, message: '用户权限必须选择', trigger: 'change' },
        ],
        address: [
          { max: 100, message: '单位地址长度不能超过100个字符', trigger: 'blur' },
        ],
        phone: [
          { required: true,  message: '管理员手机号不能为空', trigger: 'blur' },
          { validator: validatorTel, trigger: 'blur' }

        ],
        password: [
          { required: true, message: '单位密码不能为空', trigger: 'blur' },
          { max:12, message: '管理员手机号不能超过12个字符', trigger: 'blur' },
        ],

      }
    }
  },
  methods: {
    init(){
      console.log(this.editorData,'this.editorData')
      this.formValidate=this.isAdd?{
        orgId:'',
        name:'',
        // sex:'',
        type:'',
        address:'',
        regionCode:[],
        phone:'',
        password:'',
        id:'',
      }:{...this.editorData}
    },
    getSumbitParm(){
      return {
        ...this.formValidate,
        province :_.get(this.formValidate,'regionCode.0')||'',
        city:_.get(this.formValidate,'regionCode.1')||'',
        district:_.get(this.formValidate,'regionCode.2')||'',
      }
    },
    async Add(){
      return  await userAdd(this.getSumbitParm());
    },
    async updata(){
      return await userUpdate(this.getSumbitParm())
    },
    handleSubmit (name) {
      this.$refs[name].validate(async (valid) => {
        if (valid) {
          let res = this.isAdd? await this.Add():await this.updata();
          if(_.get(res,'code')==0){
            this.$Message.success(this.isAdd?'新增成功':'修改成功');
            this.$emit('success');
          }else{
            this.$Message.error(_.get(res,'message'));
          }

        } else {
          // this.$Message.error('Fail!');
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields();
    }
  }
}
</script>
<style scoped lang="less">

</style>
